package com.nursing.think.fragment;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nursing.think.MainActivity;
import com.nursing.think.R;
import com.nursing.think.adapter.SpinnerAdapter;
import com.nursing.think.utils.DisplayUtil;
import com.nursing.think.utils.ToastUtil;
import com.nursing.think.view.CustomSpinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * 题库
 *
 * @author: liyunsheng
 * Date: 2021-11-23
 * Time: 14:51
 */

public class QuestionBank extends Fragment {
    private CustomSpinner spinner;
    private ImageView spinnerImg;
    private String[] mArrayString = null;
    private TextView insistTextView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater .inflate(R.layout.fragment_question_bank ,container,false) ;
        initView(view);
        initData();
        onViewClicks();
        return view;
    }

    private void onViewClicks() {
//        spinner设置监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ToastUtil.showToast(getActivity(),"你选的是的是第"+position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner.setSpinnerEventsListener(new CustomSpinner.OnSpinnerEventsListener() {
            @Override
            public void onSpinnerOpened() {
                //展开
                spinnerImg.setRotation(-90);
            }

            @Override
            public void onSpinnerClosed() {
                spinnerImg.setRotation(0);
            }
        });

    }
    private void initData() {
        mArrayString = new String[]{"android讲义","java面向对象","王者归来"};
        spinner.setAdapter(new SpinnerAdapter(getActivity(), mArrayString));

    }
    private void initView(View view) {
        spinner = view.findViewById(R.id.spinner);
        spinnerImg = view.findViewById(R.id.spinnerImg);



        insistTextView = view.findViewById(R.id.insistTextView);
        String text = "15天";
        int start = text.indexOf("天");
        int end = text.length();
        SpannableString textSpan = new SpannableString (text);
        textSpan.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(getActivity(),22)),0,start, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(getActivity(),12)),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        insistTextView.setText(textSpan);
    }
}
